//
//  CSWNavigationController.m
//  网易彩票
//
//  Created by MAC on 16/6/5.
//  Copyright © 2016年 MAC. All rights reserved.
//

#import "CSWNavigationController.h"

#import "UIImage+Image.h"

@interface CSWNavigationController ()<UINavigationControllerDelegate>

@property (nonatomic, strong) id popDelegate;

@end

@implementation CSWNavigationController

+ (void)initialize{
    if(self == [CSWNavigationController class]){
        // 获取当前整个应用程序下的所有导航条的外观
        // UINavigationBar *navBar = [UINavigationBar appearance];
        
        // 获取当前类下面的导航条
        UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedIn:self, nil];
        
        [navBar setBackgroundImage:[UIImage imageNamed:@"NavBar64"] forBarMetrics:UIBarMetricsDefault];
        
        // 创建描述字体的字典
        NSMutableDictionary *textAttr = [NSMutableDictionary dictionary];
        // 颜色
        textAttr[NSForegroundColorAttributeName] = [UIColor whiteColor];
        textAttr[NSFontAttributeName] = [UIFont boldSystemFontOfSize:17];
        
        // titleTextAttributes:给标题文字设置属性,(颜色,字体,阴影....)
        navBar.titleTextAttributes = textAttr;
    }
}

- (void)viewDidLoad{
    _popDelegate = self.interactivePopGestureRecognizer.delegate;
    
    self.delegate = self;
    
}

//当家在完一个控制器的时候调用
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    if (viewController == self.childViewControllers[0]) {
        // 回到根控制器
        self.interactivePopGestureRecognizer.delegate = _popDelegate;
    }else{ // 不是根控制器
        self.interactivePopGestureRecognizer.delegate = nil;
        
    }
}

// 在push的时候,设置下一个栈顶控制器的导航条的左边按钮
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    
    [super pushViewController:viewController animated:animated];
    
    // 不是导航控制器的根控制器才需要设置返回按钮
    if (self.childViewControllers.count > 1) { // 不是根控制器
        
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalImageName:@"NavBack"] style:0 target:self action:@selector(back)];
    }
}

- (void)back{
    [self popViewControllerAnimated:YES];
}


//- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {
//    /**
//     *  这里有两个条件不允许手势执行，1、当前控制器为根控制器；2、如果这个push、pop动画正在执行（私有属性）
//     */
//    return self.viewControllers.count != 1 && ![[self valueForKey:@"_isTransitioning"] boolValue];
//}

@end










